package com.zzyl.common.ai;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.chat.ChatResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Author:zhq
 * @Date: 2025/08/16 19:49
 * @Description:
 */
@Component
@Slf4j
public class AIModelInvoker{
    @Autowired
    private BaiduAIProperties baiduAIProperties;

    public String qianfanInvoker(String prompt) {
        try {
            Qianfan qianfan = new Qianfan(baiduAIProperties.getAccessKey(), baiduAIProperties.getSecretKey());
            ChatResponse response = qianfan.chatCompletion()
                    .model(baiduAIProperties.getQianfanModel())
                    .temperature(0.6)
                    .maxOutputTokens(2048)
                    .responseFormat("json_object")
                    .addMessage("user", prompt)
                    .execute();
            log.info("调用千帆模型成功,结果:{}",response);
            log.info("调用千帆模型成功,结果:{}",response);

            return response.getResult();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("调用千帆模型失败", e);
            throw new RuntimeException(e);
        }
    }
}
